layui.use(['form','layer','laydate','table','laytpl'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laydate = layui.laydate,
        laytpl = layui.laytpl,
        table = layui.table;

    //新闻列表
    var tableIns = table.render({
        elem: '#goods_list',
        url : '/listGoods',
        cellMinWidth : 95,
        page : true,
        height : "full",
        limit : 20,
        limits : [20],
        id : "goods_list_table",
        cols : [[
            {field: 'id', title: 'ID', width:60, align:"center"},
            {field: 'name', title: '分类', align:'center',width:150},
            {field: 'categoryId', title: '商品名称', align:'center',width:200},
            {field: 'miniPrice', title: '最低价', align:'center',width:100},
            {field: 'originalPrice', title: '原始价', align:'center',width:100},
            {field: 'numberOrders', title: '购买次数', align:'center',width:100},
            {field: 'numberGoodReputation', title: '好评次数', align:'center',width:100},
            {field: 'status', title: '状态', align:'center',width:100},
            {field: 'stores', title: '库存', align:'center',width:100},
            {field: 'weight', title: '重量', align:'center',width:100},
            {field: 'logisticsId', title: '运费', align:'center',width:100},
            {field: 'displayOrder', title: '排序', align:'center',width:100},
            {title: '操作', width:170, templet:'#goods_list_bar',fixed:"right",align:"center"}
        ]],
        response:{
            countName: 'total' //数据总数的字段名称，默认：count
            ,dataName: 'data' //数据列表的字段名称，默认：data
        }
    });

    //搜索【此功能需要后台配合，所以暂时没有动态效果演示】
    $(".search_btn").on("click",function(){
        if($(".searchVal").val() != ''){
            table.reload("goods_list_table",{
                page: {
                    curr: 1 //重新从第 1 页开始
                },
                where: {
                    key: $(".searchVal").val()  //搜索的关键字
                }
            })
        }else{
            layer.msg("请输入搜索的内容");
        }
    });

    //添加文章
    function addGoods(edit){
        var index = layui.layer.open({
            title : "添加商品",
            type : 2,
            content : "goodsAdd.html",
            success : function(layero, index){
                var body = layui.layer.getChildFrame('body', index);
                if(edit){
                    $.post("/findContentByGoodsId",{goodsId:edit.id},function(res){
                        body.find("input[name=id]").val(edit.id);
                        body.find(".name").val(edit.name);
                        body.find(".categoryId").attr("data-value",edit.categoryId);
                        body.find(".miniPrice").val(edit.miniPrice);
                        body.find(".originalPrice").val(edit.originalPrice);
                        body.find(".displayOrder").val(edit.displayOrder);

                        body.find("input[name=picUrl]").val(edit.picUrl);
                        body.find(".thumbImg").attr("src",edit.picUrl);

                        body.find(".status").attr("data-value",edit.status);
                        body.find(".stores").val(edit.stores);
                        body.find(".weight").val(edit.weight);
                        body.find(".logisticsId").attr("data-value",edit.logisticsId);

                        body.find("#goods_content").val(res.data);

                        form.render();
                    });
                }
                setTimeout(function(){
                    layui.layer.tips('点击此处返回商品列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500)
            }
        })
        layui.layer.full(index);
        //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
        $(window).on("resize",function(){
            layui.layer.full(index);
        })
    }
    $(".addGoods_btn").click(function(){
        addGoods();
    });

    //列表操作
    table.on('tool(goods_list)', function(obj){
        var layEvent = obj.event,
            data = obj.data;

        if(layEvent === 'edit'){ //编辑
            addGoods(data);
        } else if(layEvent === 'del'){ //删除
            layer.confirm('确定删除此商品吗？',{icon:3, title:'提示信息'},function(index){
                $.get("/deleteGoodsById",{
                    goodsId : data.id  //将需要删除的newsId作为参数传入
                },function(res){
                    tableIns.reload();
                    layer.close(index);
                })
            });
        }
    });

});